package com.rivulet.dawn.service.service.business.impl;

import com.rivulet.dawn.api.request.business.TagSaveRequest;
import com.rivulet.dawn.api.request.business.TagSearchRequest;
import com.rivulet.dawn.api.response.business.TagSearchResponse;
import com.rivulet.dawn.api.tools.EmptyTool;
import com.rivulet.dawn.api.tools.Page;
import com.rivulet.dawn.service.mapper.EventRecordMapper;
import com.rivulet.dawn.service.mapper.TagMapper;
import com.rivulet.dawn.service.service.business.TagService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Service
public class TagServiceImpl implements TagService {
    @Resource
    private TagMapper tagMapper;

    @Override
    public Page<TagSearchResponse> search(TagSearchRequest tagSearchRequest) {
        if (Objects.isNull(tagSearchRequest)) {
            return EmptyTool.emptyPage();
        }
        Long total = tagMapper.total(tagSearchRequest);
        if (Objects.isNull(total) || total <= 0) {
            return EmptyTool.emptyPage();
        }
        List<TagSearchResponse> responseList = tagMapper.search(tagSearchRequest);
        return new Page<>(total, responseList);
    }

    @Override
    public Integer save(TagSaveRequest tagSaveRequest) {
        if (Objects.isNull(tagSaveRequest)) {
            return 0;
        }
        if (Objects.nonNull(tagSaveRequest.getId())) {
            tagMapper.update(tagSaveRequest);
        } else {
            tagMapper.insert(tagSaveRequest);
        }
        return 1;
    }
}
